package com.itita.weibo.model;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import weibo4andriod.Status;
import weibo4andriod.User;
import weibo4andriod.WeiboException;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.itita.weibo.manager.MainService;
import com.itita.weibo.ui.ListAllFriendsActivity;
import com.itita.weibo.ui.ListMyFansActivity;
import com.itita.weibo.ui.R;

public final class FansAdapter extends  BaseAdapter{
   
	private Context context;
	
	private List<User> fans;
	
	private LayoutInflater inflater;
	
	private FansHodler fansHodler;
	
	public FansAdapter(Context context, List<User> friends) {
		super();
		this.context = context;
		this.fans = friends;
        this.fansHodler = new FansHodler();
		this.inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return this.fans.size() + 2;
	}

	@Override
	public Object getItem(int position) {
		if (position == 0){
			return null;
		}else if (position == this.getCount() - 1){
			return null;
		}
		// 返回这一条用户信息
		else{
			return this.fans.get(position - 1);
		}
	}

	@Override
	public long getItemId(int position) {
		if (position == 0){// 刷新
			return 0;
		} else if (position > 0 && position < this.getCount() - 1){// 返回微博的ID
			return this.fans.get(position - 1).getId();
		} else { // 选中了更多
			return -1;
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
 
//		
		if(position == 0){ //刷新
			View userItem = LayoutInflater.from(context).inflate(R.layout.moreitemsview, null);
			TextView tv = (TextView) userItem.findViewById(R.id.tvItemContent);
			tv.setText("刷新");
			return userItem;
		}else if(position == this.getCount() - 1){ //更多
			View userItem = LayoutInflater.from(context).inflate(R.layout.moreitemsview, null);
			TextView tv = (TextView) userItem.findViewById(R.id.tvItemContent);
			tv.setText("更多");
			return userItem;
		}else{
			View userItem = null;
			if (convertView != null && convertView.findViewById(R.id.tvItemName) != null) {
				userItem = convertView;
			}else {
				// 把xml布局文件变成View对象
				userItem = this.inflater.inflate(R.layout.item_fans, null);
			}
			this.initUserHoler(userItem);
			final User user = this.fans.get(position-1);
			try {
				this.fansHodler.tvUsername.setText(user.getName());
				this.fansHodler.tvDesctption.setText(user.getDescription());
				try {
					if(MainService.mainService.weibo.existsFriendship(String.valueOf(user.getId()), String.valueOf(MainService.currentUser.getId()))){
						fansHodler.btnAttentionState.setText("取消关注");
						fansHodler.btnAttentionState.setBackgroundResource(R.drawable.attend_cancel);
					}
					else{
						fansHodler.btnAttentionState.setText("恢复关注");
						fansHodler.btnAttentionState.setBackgroundResource(R.drawable.attend_do);
					}
				} catch (WeiboException e) {
					e.printStackTrace();
				}
				/**
				 * 按键监听器
				 */
				
				fansHodler.btnAttentionState.setOnClickListener(new OnClickListener(){
					@Override
					public void onClick(View v) {
//						Button	bt = (Button)v;
						String	text = ((Button)v).getText().toString();
//						String idfans = new Integer(user.getId()).toString();
						
						//传送参数为 用户ID 还有 按钮
						HashMap<String, Object> taskParam = new HashMap<String, Object>();
						taskParam.put("userId", String.valueOf(user.getId()));
						taskParam.put("button",v);
//						////System.out.println("传送参数为 :"  +taskParam.toString());
						
						
						Task tsAttendCancel = null;
						if(text.equals("取消关注")){
//							////System.out.println("取消关注监听:"  +" Name:"+ user.getName() +" ID:"+ user.getId());
							//更新界面进度条
							MainService.getActivityByName("ListMyFansActivity").refresh(ListMyFansActivity.REF_FANS_CANCEL_BEGIN, null);
					    	tsAttendCancel = new Task(Task.TASK_FANS_CANCEL, taskParam);
							
						}
						else if(text.equals("恢复关注")){
//							////System.out.println("恢复关注监听:"  +" Name:"+ user.getName() +" ID:"+ user.getId());

							//更新界面进度条
							MainService.getActivityByName("ListMyFansActivity").refresh(ListMyFansActivity.REF_FANS_RESTORE_BEGIN, null);
					    	tsAttendCancel = new Task(Task.TASK_FANS_RESTORE, taskParam);
						}
						
						
				    	//添加取消关注任务
				    	if(tsAttendCancel != null){
					    	MainService.newTaskToTaskQueneHead(tsAttendCancel);
				    	}
						
					}
					
				 }
				);
				Bitmap userIconBitmap = MainService.mainService.allIcon.get(user.getId());
				if (userIconBitmap != null) {
					this.fansHodler.ivUserIcon.setImageBitmap(userIconBitmap);
				} else {
					this.fansHodler.ivUserIcon.setBackgroundResource(R.drawable.portrait);
				}
			} catch (Exception e) {
				e.printStackTrace();
				Log.e("ERROR", "加载好友出错了");
			}
			return userItem;	
		}
		
		
	}
    
	private void initUserHoler(View userItem) {
		this.fansHodler.tvUsername = (TextView) userItem.findViewById(R.id.tvUsername);
		this.fansHodler.tvDesctption = (TextView) userItem.findViewById(R.id.tvDesctption);
		this.fansHodler.ivUserIcon =  (ImageView) userItem.findViewById(R.id.ivUserIcon);
		this.fansHodler.btnAttentionState = (Button) userItem.findViewById(R.id.ivAttentionState);
	}

	/**
	 * 动态给AttentionAdapter添加数据
	 * @param datas
	 */
	public void addMoreData(List<User> datas){
		this.fans.addAll(datas);
//		dddd
		for (Iterator iterator = datas.iterator(); iterator.hasNext();) {
			User user = (User) iterator.next();
			if(!this.fans.contains(user)){
				this.fans.add(user);
			}
		}
		this.notifyDataSetChanged();
	}
}
